﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Net.NetworkInformation;
using System.Windows.Threading;
using System.ComponentModel;
using System.IO.IsolatedStorage;

namespace Curling
{
	public partial class SystemTray : UserControl
    {
        BackgroundWorker CheckConnectionStatusWorker;

        Microsoft.Phone.Net.NetworkInformation.NetworkInterfaceType networkInterfaceType;

        DispatcherTimer Time;

        public bool IsSystemTrayExpanded = true;

        private bool isSoundOn;
        public bool IsSoundOn
        {
            get
            {
                return isSoundOn;
            }
            set
            {
                isSoundOn = value;

                if (isSoundOn)
                {
                    VisualStateManager.GoToState(this, "SoundOn", false);
                }
                else
                {
                    VisualStateManager.GoToState(this, "SoundOff", false);
                }
            }
        }
        
        private bool isAutoScroll;
        public bool IsAutoScroll
        {
            get
            {
                return isAutoScroll;
            }
            set
            {
                isAutoScroll = value;

                if (isAutoScroll)
                {
                    VisualStateManager.GoToState(this, "ScrollAuto", false);
                }
                else
                {
                    VisualStateManager.GoToState(this, "ScrollManual", false);
                }
            }
        }

        private bool isNewGameEnabled;
        public bool IsNewGameEnabled
        {
            get
            {
                return isNewGameEnabled;
            }
            set
            {
                isNewGameEnabled = value;

                if (isNewGameEnabled)
                {
                    VisualStateManager.GoToState(this, "NewGameVisible", true);
                }
                else
                {
                    VisualStateManager.GoToState(this, "NewGameCollapsed", true);                    
                }
            }
        }

        private bool isReplayEnabled;
        public bool IsReplayEnabled
        {
            get
            {
                return isReplayEnabled;
            }
            set
            {
                isReplayEnabled = value;

                if (isReplayEnabled)
                {
                    VisualStateManager.GoToState(this, "ReplayVisible", true);
                }
                else
                {
                    VisualStateManager.GoToState(this, "ReplayCollapsed", true);
                }
            }
        }

        public Score Score { get; set; }

        public delegate void ScrollEventHandler(object sender, EventArgs e);
        public event ScrollEventHandler ScrollEvent;

        public delegate void NewGameEventHandler(object sender, EventArgs e);
        public event NewGameEventHandler NewGameEvent;

        public delegate void ReplayEventHandler(object sender, EventArgs e);
        public event ReplayEventHandler ReplayEvent;

		public SystemTray()
		{
			InitializeComponent();

            VisualStateManager.GoToState(this, "SystemTrayCollapsed", false);

            VisualStateManager.GoToState(this, "CellularCollapsed", false);
            VisualStateManager.GoToState(this, "FirstPlayerActive", false);
            VisualStateManager.GoToState(this, "WiFiCollapsed", false);
            VisualStateManager.GoToState(this, "ReplayCollapsed", false);
            VisualStateManager.GoToState(this, "NewGameCollapsed", false);

            IsAutoScroll = true;
            IsSoundOn = true;

            CheckConnectionStatusWorker = new BackgroundWorker();
            CheckConnectionStatusWorker.DoWork += new DoWorkEventHandler(CheckConnectionStatusWorker_DoWork);
            CheckConnectionStatusWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(CheckConnectionStatusWorker_RunWorkerCompleted);
		}

        void CheckConnectionStatusWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            networkInterfaceType = Microsoft.Phone.Net.NetworkInformation.NetworkInterface.NetworkInterfaceType;                
        }

        void CheckConnectionStatusWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            switch (networkInterfaceType)
            {
                case Microsoft.Phone.Net.NetworkInformation.NetworkInterfaceType.Wireless80211:
                case Microsoft.Phone.Net.NetworkInformation.NetworkInterfaceType.Ethernet:
                {
                    VisualStateManager.GoToState(this, "CellularCollapsed", true);
                    VisualStateManager.GoToState(this, "WiFiVisible", true);

                    break;
                }
                case Microsoft.Phone.Net.NetworkInformation.NetworkInterfaceType.MobileBroadbandCdma:
                case Microsoft.Phone.Net.NetworkInformation.NetworkInterfaceType.MobileBroadbandGsm:
                {
                    VisualStateManager.GoToState(this, "CellularVisible", true);
                    VisualStateManager.GoToState(this, "WiFiCollapsed", true);

                    break;
                }
                case Microsoft.Phone.Net.NetworkInformation.NetworkInterfaceType.None:
                {
                    VisualStateManager.GoToState(this, "CellularCollapsed", true);
                    VisualStateManager.GoToState(this, "WiFiCollapsed", true);

                    break;
                }
            }
        }

		private void UserControl_Loaded(object sender, System.Windows.RoutedEventArgs e)
		{
            TimeText.Text = DateTime.Now.ToString("HH:mm");

            Player1Score.DataContext = Score;
            Player2Score.DataContext = Score;

            Time = new DispatcherTimer()
            {
                Interval = TimeSpan.FromSeconds(30)
            };

            Time.Tick += new EventHandler(Time_Tick);
            Time.Start();

            NetworkChange.NetworkAddressChanged += new NetworkAddressChangedEventHandler(NetworkChange_NetworkAddressChanged);

            CheckConnection();

            VisualStateManager.GoToState(this, "SystemTrayVisible", true);
		}

        void Time_Tick(object sender, EventArgs e)
        {
            Dispatcher.BeginInvoke(() =>
            {
                TimeText.Text = DateTime.Now.ToString("HH:mm");
            });

            CheckConnection();
        }

        void NetworkChange_NetworkAddressChanged(object sender, EventArgs e)
        {
            CheckConnection();
        }

        private void CheckConnection()
        {
            if (!CheckConnectionStatusWorker.IsBusy)
            {
                CheckConnectionStatusWorker.RunWorkerAsync();
            }
        }

        private void LayoutRoot_ManipulationStarted(object sender, System.Windows.Input.ManipulationStartedEventArgs e)
        {
            //Timer.Start();
        }

        public void Expand()
        {
            if (!IsSystemTrayExpanded)
            {
                VisualStateManager.GoToState(this, "SystemTrayVisible", true);

                //Timer = new DispatcherTimer()
                //{
                //    Interval = TimeSpan.FromSeconds(6.0)
                //};

                //Timer.Tick += new EventHandler(Timer_Tick);
                //Timer.Start();

                IsSystemTrayExpanded = true;
            }
        }

        public void Collapse()
        {
            if (IsSystemTrayExpanded)
            {
                //Timer.Stop();

                VisualStateManager.GoToState(this, "SystemTrayCollapsed", true);

                IsSystemTrayExpanded = false;
            }
        }

        void Timer_Tick(object sender, EventArgs e)
        {
            Dispatcher.BeginInvoke(() =>
            {
                Collapse();
            });
        }

        private void SoundButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            IsSoundOn = !IsSoundOn;

            SoundManager.Mute = !IsSoundOn;
           
            //Timer.Start();
        }

        private void NewGameButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (IsNewGameEnabled)
            {
                IsNewGameEnabled = false;
                NewGameEvent(this, EventArgs.Empty);
            }
        }

        private void ScrollButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            ScrollEvent(this, EventArgs.Empty);
        }

        public void ChangePlayer(PlayerColor playerColor)
        {
            if (playerColor == PlayerColor.Yellow)
            {
                VisualStateManager.GoToState(this, "FirstPlayerActive", false);
            }
            else
            {
                VisualStateManager.GoToState(this, "SecondPlayerActive", false);
            }
        }

        private void ReplayButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (IsReplayEnabled)
            {
                ReplayEvent(this, EventArgs.Empty);
            }
        }
	}
}